package com.sztx.pay.center.worker.quartz;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import org.mortbay.log.Log;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.sztx.pay.center.common.util.Constance.CallBackOrderState;
import com.sztx.pay.center.core.service.CallBackService;
import com.sztx.pay.center.dataaccess.dao.mysql.CallbackNotifyMysqlDAO;
import com.sztx.pay.center.dataaccess.domain.CallbackNotifyDO;
import com.sztx.se.common.util.date.DateUtil;
import com.sztx.se.core.quartz.job.BaseJob;

@Component("notifyJob")
public class NotifyJob extends BaseJob {
	//第一次回调时间间隔 单位秒
	private int firstTimeInterval = 30;
	//第二次回调时间间隔
	private int secondTimeInterval = 60;
	//第3次回调时间间隔
	private int thirdTimeInterval = 600;
	
	protected final Logger LOG = LoggerFactory.getLogger(NotifyJob.class);
	
	@Autowired
	CallbackNotifyMysqlDAO callbackNotifyMysqlDAO;
	@Autowired
	CallBackService callBackService;

	@Override
	public Object executeJob(JobExecutionContext arg0) throws JobExecutionException {

		Log.info("=====================batch notify start======================");
		Date endNotifyTime = new Date(); 
		Date startNotifyTime = getBeforeDate(endNotifyTime, thirdTimeInterval + 70); //
		Log.info("startNotifyTime:"+startNotifyTime+"   endNotifyTime:"+endNotifyTime);
		List<CallbackNotifyDO> callbackNotifyDOList = callbackNotifyMysqlDAO.findByNotifyTime(DateUtil.formatDate(startNotifyTime, DateUtil.LONG_DATE_FORMAT_STR));
		List<CallbackNotifyDO> callbackNotifyDOList2 = new ArrayList<CallbackNotifyDO>();
		
		
		
		List<CallbackNotifyDO> updateStatusList = new ArrayList<CallbackNotifyDO>();
		for (int i = 0; i < callbackNotifyDOList.size(); i++) {
			CallbackNotifyDO tmpDo = callbackNotifyDOList.get(i);
			if(checkTimeAvalible(tmpDo)){
				tmpDo.setStatus((byte)CallBackOrderState.ING.getCode());
				updateStatusList.add(tmpDo);
				callbackNotifyDOList2.add(callbackNotifyDOList.get(i));
			}
		}
		if(updateStatusList.size() >= 1){
			callbackNotifyMysqlDAO.updateState(updateStatusList );
		}
		
		
		if(callbackNotifyDOList2 == null || callbackNotifyDOList2.size() < 1){
			Log.info("there is no order need to notify");
			return null;
		}
		for (int i = 0; i < callbackNotifyDOList2.size(); i++) {
			callBackService.callBack(callbackNotifyDOList2.get(i));
		}
		return null;
	}
	private boolean checkTimeAvalible(CallbackNotifyDO callbackNotifyDO){
		Date now = new Date();
		int count = callbackNotifyDO.getCount();
		Date nextNotifyTime = callbackNotifyDO.getNextNotifyTime();
		if(count == 0){
			return true;
		}else if(count == 1){
			Date secondCallBackTime = getBeforeDate(now, secondTimeInterval);
			if(nextNotifyTime.before(secondCallBackTime)){
				return true;
			}else{
				return false;
			}
		}else if(count == 2){
			Date thirdCallBackTime = getBeforeDate(now, thirdTimeInterval);
			if(nextNotifyTime.before(thirdCallBackTime)){
				return true;
			}else{
				return false;
			}
		}
		return false;
	}
	
	private Date getBeforeDate(Date date,int seconds){
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		
		int second = c.get(Calendar.SECOND);
		c.set(Calendar.SECOND, second - seconds);
		
		return c.getTime();
	}
}
